#include "RectCont.h"

//pushes a rectangle to the containers
RectCont::RectCont()
{
	for (int i=0; i<MAX_RECT; i++)
		rectCont[i] = NULL;
	m_counter = 0;
}

//returns the index of the rectangle that includes the searched point otherwise returns -1
int RectCont::findRectIndex(Point& point)
{
	int i = 0;
	bool isFound = false;

	while (i < MAX_RECT && !(isFound = rectCont[i]->isRectPoint(point)))
	{
		i++;
	}
	if (isFound)
		return i;
	else
		return -1;
}

//checks if the rectangle container is full: true:full , false:not full
bool RectCont::isContFull()
{
	if (m_counter == MAX_RECT)
		return true;
	else
		return false;
}

//adds rectangle to the container and returns true if it's succeed, gets point, width and length for addition
bool RectCont::addRect(Point& point, int length, int width)
{
	if (!isContFull())
	{
		rectCont[m_counter] = new Rect;
		rectCont[m_counter++]->setRect(point, length, width);
		return true;
	}
	else
		return false;
}

//adds rectangle to the container and returns true if it's succeed, gets rectangle for addition
bool RectCont::addRect(Rect& rect)
{
	if (!isContFull())
	{
		rectCont[m_counter] = new Rect;
		*rectCont[m_counter++] = rect;
		return true;
	}
	else
		return false;
}

//srinks the container (moving empty pointers to the start), gets an index of the empty pointer
void RectCont::shrinkRect(int emptyIndex)
{
	for (int i=emptyIndex; i<MAX_RECT-1; i++)
		if (rectCont[i+1] != NULL)
		{
			rectCont[i] = rectCont[i+1];
			rectCont[i+1] = NULL;
		}
}

//removes rectangle, get a point
int RectCont::removeRect(Point& point)
{
	int remIndex = -1;

	remIndex = findRectIndex(point);
	if (remIndex != -1)
	{
		rectCont[remIndex] = NULL;
		shrinkRect(remIndex);
	}
	return remIndex;
	
}

//shifts rectangle, gets a point and increments values for x and y 
bool RectCont::shiftRect(Point& point, int incX, int incY)
{
	int recIndex = NULL;

	if ((recIndex = findRectIndex(point)) != -1)
	{
		rectCont[recIndex]->moveRectPoint(incX, incY);
		return true;
	}
	else
		return false;
}

//brings the rectangle to the front by moving it to the end of the container, returns true for success
bool RectCont::bringRectToFront(Point& point)
{
	int rectFoundIndex = NULL;
	Rect* rectPtr = NULL;
	
	rectFoundIndex = findRectIndex(point);	//finds the index of the selected rectangle
	if (rectFoundIndex > 0)	// rectFoundIndex > 0 -> the selected rectangle was found
	{
		rectPtr = rectCont[rectFoundIndex];	//points to the selected rectangle
		rectCont[rectFoundIndex] = NULL;	//puts NULL to the pointer cell of the selected container
		shrinkRect(rectFoundIndex);	//shrinks the conainer
		if (!addRect(*rectPtr))	//adds the rectangle to the end of the container
		{
			cerr << "Error - failed to add rectangle to the container." << endl;	//in a case that the adding failed
			return false;
		}
		return true;
	}
	else	//in a case that the selected rectangle wasn't found
		return false;
}

//brings the rectangle to the back by moving it to the beginning of the container, returns true for success
bool RectCont::bringRectToBack(Point& point)
{
	int rectFoundIndex = NULL;
	Rect* rectPtr = NULL;
	
	rectFoundIndex = findRectIndex(point);	//finds the index of the selected rectangle
	if (rectFoundIndex > 0)	// rectFoundIndex > 0 -> the selected rectangle was found
	{
		rectPtr = rectCont[rectFoundIndex];	//points to the selected rectangle
		rectCont[rectFoundIndex] = NULL;	//puts NULL to the pointer cell of the selected container
		for (int i=rectFoundIndex; i>0; i--)	//shrinks the rectangles from the bginning to the selected rectnagles
			rectCont[i] = rectCont[i-1];
		rectCont[0] = rectPtr;	//puts the selected rectangle in the beginning
		return true;
	}
	else	//in a case that the selected rectangle wasn't found
		return false;	
}

//prints all the rectangles acording to ch
void RectCont::drawAll(char ch, bool isFullFilling)
{
	clrscr();
	for (int i=0; i<m_counter; i++)
		rectCont[i]->drawRect(ch, isFullFilling);
}

//prints all the rectangles acording to ch and prints a special rectangle acording chSpec, returns true if if the spcial rectangle is found
bool RectCont::drawAllExecptOneSpecial(Point& point, char chAll, char chSpec, bool isFullFilling)
{
	int i = 0;
	bool isFoundRect = false;

	while (i < m_counter)
	{
		if (isFoundRect = rectCont[i]->isRectPoint(point))
			rectCont[i]->drawRect(chSpec, isFullFilling);
		else
			rectCont[i]->drawRect(chAll, isFullFilling);
		i++;
	}
	return isFoundRect;
}

//empty destructor 
RectCont::~RectCont()
{
}

